package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.qf.util.CookieUtil;
import com.qf.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/auth")
public class LoginController {
    //将来JWT令牌保存在Cookie 名为token
    public static final String TOKEN = "token";

    @Value("${custom.username}")
    private String customUsername;
    @Value("${custom.password}")
    private String customPassword;
    /**
     *
     * custom:
     *   # 自定义认证管理员用户名
     *   username: admin
     *   # 自定义认证管理员密码
     *   password: 123456
     * http://localhost:8080/auth/login?username=admin&password=123456
     * 登录
     */
    @GetMapping("/login")
    public String login(String username, String password, HttpServletResponse response){

        // 成功了：
        //生成JWT令牌
        Set<String> roles = new HashSet<>();
        roles.add("管理员1");
        roles.add("管理员2");
        roles.add("管理员3");
        roles.add("管理员4");
        String jwt = JwtUtil.
                createJWT(UUID.randomUUID().toString(), JSONObject.toJSONString(roles), null);
        //添加Cookie
        CookieUtil.addCookie(response,"localhost","/",TOKEN,jwt,3600000,false);
        return "OK";
    }
}
